package controller;

import dao.DisciplinaJpaController;
import dao.EmentaJpaController;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import dao.exceptions.NonexistentEntityException;
import java.util.ArrayList;
import model.Disciplina;
import model.DisciplinaProfessor;
import model.Ementa;
import model.Professor;
import pagecontroller.JsfUtil;
import pagecontroller.PageManager;

/**
 *
 * @author Edilson Fernandes da Costa
 * @version 1.0
 */
public class DisciplinaController extends Controller {

    public DisciplinaController() {
        if (JsfUtil.getObjectFromSession("DisciplinaJpaController") == null) {
            jpaController = new DisciplinaJpaController();
        } else {
            jpaController = (DisciplinaJpaController) JsfUtil.getObjectFromSession("DisciplinaJpaController");
        }
    }
    private Disciplina disciplina = null;
    private Disciplina disciplinaAux = null;
    private DisciplinaJpaController jpaController = null;
    private List<Disciplina> disciplinas = null;
    private Ementa ementaTemp = null;
    private DisciplinaProfessor disciplinaProfessor;
    private List<Ementa> ementas;

    public void clear() {
        disciplina = null;
        disciplinaProfessor = null;
        ementas = null;
    }

    public String setPageDisciplinaCad() {
        clear();
        getPageManager().setPageDisciplinaCad();
        return "refreshPage";
    }

    public Ementa getEmentaTemp() {
        if (ementaTemp == null) {
            ementaTemp = new Ementa();
        }
        return ementaTemp;
    }

    public void setEmentaTemp(Ementa ementaTemp) {
        this.ementaTemp = ementaTemp;
    }

    public String persiste() {
        if (disciplina.getId() == null) {
            return create();
        } else {
            return update();
        }
    }

    public String update() {
        try {
            jpaController.edit(this.disciplina);

            for (Ementa ementa : getEmentas()) {
                if (ementa.getId() == null) {
                    getEmentaJpaController().create(ementa);
                } else {
                    getEmentaJpaController().edit(ementa);
                }
            }
            disciplinaAux = disciplina;
            this.disciplina = null;
            JsfUtil.addSuccessMessage("Disciplina Atualizado com Sucesso");
            getPageManager().setPageDisciplinaRelat();
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(DisciplinaController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(DisciplinaController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "refreshPage";
    }

    private EmentaJpaController getEmentaJpaController() {
        EmentaJpaController ementaJpaController = (EmentaJpaController) JsfUtil.getObjectFromSession("EmentaJpaController");
        if (ementaJpaController == null) {
            ementaJpaController = new EmentaJpaController();
        }

        return ementaJpaController;
    }

    public String create() {
        try {

            if (findDisciplina(getDisciplina().getNome()) != null) {
                disciplina = findDisciplina(getDisciplina().getNome());
            } else {
                jpaController.create(disciplina);
            }
            //disciplina.setProfessor(getUsuarioLogado().getProfessor());

            setDisciplinaProfessor(new DisciplinaProfessor());
            getDisciplinaProfessor().setDisciplina(disciplina);
            getDisciplinaProfessor().setProfessor(getUsuarioLogado().getProfessor());
            getDisciplinaProfessorController().getJpaController().create(getDisciplinaProfessor());

            for (Ementa ementa : getEmentas()) {
                ementa.setDisciplinaProfessor(getDisciplinaProfessor());
                getEmentaJpaController().create(ementa);
            }

            disciplinaAux = disciplina;
            disciplina = null;
            JsfUtil.addSuccessMessage("Disciplina cadastrado com sucesso.");
            getPageManager().setPageDisciplinaRelat();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "Ocorreu um erro na percistencia dos dados.");
            return null;
        }
        return "refreshPage";
    }

    public String edit() {
        Disciplina disciplinaTemp = (Disciplina) JsfUtil.getObjectFromRequestParameter("disciplinaTemp");
        this.disciplina = disciplinaTemp;
        return "refreshPage";
    }

    public String destroy() {
        Disciplina disciplinaTemp = (Disciplina) JsfUtil.getObjectFromRequestParameter("disciplina");
        try {
            jpaController.destroy(disciplinaTemp.getId());
            JsfUtil.addSuccessMessage("Disciplina removido com sucesso.");
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(DisciplinaController.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "refreshPage";
    }

//    public String addTurma() {
//        getPageManager().setPageTurmaCad();
//        return "refreshPage";
//    }
    public void addEmenta() {
        getEmentas().add(ementaTemp);
//        getDisciplinaProfessor().getEmentas().add(ementaTemp);
        ementaTemp = null;
        getPageManager().setModalVisible(false);

        //return "refreshPage";
    }

    public String destroyButton() {
        Disciplina disciplinaTemp = disciplinaAux;
        try {
            jpaController.destroy(disciplinaTemp.getId());
            JsfUtil.addSuccessMessage("Disciplina removida com sucesso.");
            disciplinaAux = null;

        } catch (NonexistentEntityException ex) {
            Logger.getLogger(AlunoController.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "refreshPage";
    }

    public String load() {
        disciplinaAux = (Disciplina) JsfUtil.getObjectFromRequestParameter("disciplinaTemp");


        disciplinaAux = jpaController.findDisciplina(disciplinaAux.getId());

        getPageManager().setPageDisciplinaRelat();
        return "refreshPage";
    }

    public String editButton() {
        this.disciplina = disciplinaAux;
        getPageManager().setPageDisciplinaCad();

        return "refreshPage";
    }

    public Disciplina getDisciplina() {
        if (disciplina == null) {
            disciplina = new Disciplina();
//            disciplina.setEmentas(new ArrayList<Ementa>());
        }
        return disciplina;
    }

    public List<Disciplina> getDisciplinas() {
        this.disciplinas = jpaController.findDisciplinaEntities();
        return this.disciplinas;
    }

    public List<Disciplina> getDisciplinas(Professor professor) {
        List<DisciplinaProfessor> disciplinaProfessors = getDisciplinaProfessorController().findDisciplinaProfessor(professor);

        for (DisciplinaProfessor dp : disciplinaProfessors) {
            this.disciplinas.add(dp.getDisciplina());
        }
        return this.disciplinas;
    }

    public DisciplinaJpaController getJpaController() {
        return jpaController;
    }

    public Disciplina getDisciplinaAux() {
        return disciplinaAux;
    }

    public List<Ementa> getEmentas() {
        if (ementas == null) {
            ementas = new ArrayList<Ementa>();
//            for (Ementa e : getEmentaController().finEmentas(getDisciplinaProfessor())) {
//                ementas.add(e);
//            }
        }
        return ementas;
    }

    public void setEmentas(List<Ementa> ementas) {
        this.ementas = ementas;
    }

    public Disciplina findDisciplina(Integer id) {
        return jpaController.findDisciplina(id);
    }

    private Disciplina findDisciplina(String nome) {
        for (Disciplina d : getDisciplinas()) {
            if (d.getNome().equals(nome)) {
                return d;
            }
        }
        return null;
    }

    public List<String> complete(String query) {
        List<String> results = new ArrayList<String>();


        for (Disciplina d : getDisciplinas()) {
            if (d.getNome().startsWith(query)) {
                results.add(d.getNome());
            }
        }

        return results;

    }

    public DisciplinaProfessor getDisciplinaProfessor() {
        if (disciplinaProfessor == null) {
            disciplinaProfessor = new DisciplinaProfessor();
        }
        return disciplinaProfessor;
    }

    public void setDisciplinaProfessor(DisciplinaProfessor disciplinaProfessor) {
        this.disciplinaProfessor = disciplinaProfessor;
    }
}
